package com.zjm.find.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.zjm.find.pojo.Suggestion;
import com.zjm.find.pojo.UserManage;
import com.zjm.find.service.AdminService;
import com.zjm.find.utils.JSONResult;
import com.zjm.find.utils.TimeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

/**
 * TODO
 *
 * @Author 涛涛
 * @Create 2020/2/21
 */
@Api("后台管理")
@RequestMapping("/admin")
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "后台登录", notes = "后台登录")
    @PostMapping("/login")
    public JSONResult login(UserManage userManage,
                            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                            @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) throws Exception {

        List list = adminService.selectManagers(userManage.getSuid(), userManage.getPassword());
        if (list.size() != 0) {
            PageHelper.startPage(page, limit);
            PageInfo<UserManage> pageInfo = new PageInfo<>(list);
            return JSONResult.msgAndData("登录成功", pageInfo);
        }
        return JSONResult.errorMsg("用户名或密码错误");
    }

    @ApiOperation(value = "管理员更改密码/修改信息", notes = "管理员更改密码/修改信息")
    @PostMapping("/updateManageInfoBySuid")
    public JSONResult updateManageInfoBySuid(UserManage userManage) throws Exception {
        userManage.setUpdateTime(TimeUtils.getStringTime());
        if (adminService.updateManageInfoBySuid(userManage)) {
            return JSONResult.successMsg("修改成功");
        } else {
            return JSONResult.errorMsg("修改失败");
        }
    }

    @ApiOperation(value = "查询所有管理员", notes = "查询所有管理员")
    @PostMapping("/selectAllManage")
    public JSONResult selectAllManage(UserManage userManage,
                                      @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                      @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) throws Exception {
        PageHelper.startPage(page, limit);
        List list = adminService.selectAllManage(userManage);
        if (list.size() != 0) {
            PageInfo<HashMap> pageInfo = new PageInfo<>(list);
            return JSONResult.msgAndData("查询成功", pageInfo);
        } else {
            return JSONResult.errorMsg("暂无管理员");

        }

    }

    @ApiOperation(value = "添加管理员", notes = "添加管理员")
    @PostMapping("/addManager")
    public JSONResult addManager(UserManage userManage) throws Exception {
        String[] arr = {"http://localhost:88/1.jpg", "http://localhost:88/2.jpg", "http://localhost:88/3.jpg"};
        //产生0-(arr.length-1)的整数值,也是数组的索引
        int index = (int) (Math.random() * arr.length);
        String photo = arr[index];
        userManage.setAvatar(photo);
        userManage.setCreateTime(TimeUtils.getStringTime());
        userManage.setPassword("e10adc3949ba59abbe56e057f20f883e");
        if (adminService.addManager(userManage)) {
            return JSONResult.successMsg("添加成功");
        }
        return JSONResult.errorMsg("添加失败");
    }

    @GetMapping("/selectSuggestion")
    public JSONResult selectSuggestion(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                       @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        PageInfo pageInfo = new PageInfo(adminService.selectSuggestion());
        return JSONResult.msgAndData("查询成功", pageInfo);
    }

    @PostMapping("/deleteSuggestion")
    public JSONResult deleteSuggestion(Suggestion suggestion) {
        if (adminService.deleteSuggestion(suggestion)) {
            return JSONResult.successMsg("删除成功");
        }
        return JSONResult.errorMsg("删除失败");
    }
}


